Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 1x 1x 1x 1x 4x 4x 1x 3x 3x 3x 3x 1x 2x 1x 1x 1x 1x | /**
* Admin User Enable API
* @see JCN-4 Phase 7: Wire E2E Tests to Real Backend
* @see JCN-23 Authorization fix
*/
import { NextRequest, NextResponse } from "next/server";
import { getUser, enableUser } from "@/lib/cognito-admin";
import { requireSuperAdmin, forbiddenResponse } from "@/lib/amplify-server-utils";
interface RouteContext {
params: Promise<{ id: string }>;
}
/**
* POST /api/admin/users/[id]/enable
* Enable a disabled user account
* Requires: super_admin role
*/
export async function POST(request: NextRequest, context: RouteContext) {
// Authorization check
const auth = await requireSuperAdmin();
if (!auth.authorized) {
return forbiddenResponse(auth.error);
}
try {
const { id } = await context.params;
// Check if user exists
const user = await getUser(id);
if (!user) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}
if (user.status === "Active") {
return NextResponse.json(
{ error: "User is already active" },
{ status: 400 }
);
}
await enableUser(id);
// Return updated user
const updatedUser = await getUser(id);
return NextResponse.json({
success: true,
message: "User enabled",
user: updatedUser,
});
} catch (error) {
console.error("Error enabling user:", error);
return NextResponse.json(
{ error: "Failed to enable user", details: (error as Error).message },
{ status: 500 }
);
}
}
|